package com.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.dao.BaseDao;
import com.dao.ZhengHouDao;
import com.entity.Medicine;
import com.entity.ZhengHou;
import com.entity.ZhengZhuang;

public class ZhengHouDaoImpl extends BaseDao implements ZhengHouDao {

	private Connection con = null;
	private PreparedStatement ps = null;
	@Override
	public List<ZhengHou> searchZH(String searchZZType, String searchZZValue) {
		// TODO Auto-generated method stub

		ResultSet rs = null;
		String sql = null;
		List<ZhengHou> zhlist=new ArrayList();
		ZhengHou zhenghou = null;
		if (searchZZType.equals("1")) {
			sql = "select * from zhenghou where zhname='" + searchZZValue + "'";
		}
		if (searchZZType.equals("2")) {
			Integer id = new Integer(searchZZValue);
			sql = "select * from zhenghou where zhid=" + id + "";
		}
		if (searchZZType.equals("3")) {
			sql = "select * from zhenghou where zhname like '%"+searchZZValue+"%' ";
		}
		if (searchZZType.equals("4")) {
			sql = "select zh.* from zhenghou zh,zhenghoualias zha where zha.zhid=zh.zhid and zha.zhalias='"+searchZZValue+"'";
		}
		try {
			con = this.getConn();
			ps = con.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				zhenghou = new ZhengHou();
				zhenghou.setZzId(rs.getInt("zzid"));
				zhenghou.setZhId(rs.getInt("zhid"));
				zhenghou.setZhName(rs.getString("zhname"));
				zhenghou.setZhDescript(rs.getString("zhdescript"));
				zhenghou.setmList(this.searchByZhId(zhenghou.getZhId()));
				zhenghou.setZhengzhuang(this.searchZZByZhid(zhenghou.getZzId()));
				zhlist.add(zhenghou);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			this.closeAll(con, ps, rs);
		}
		return zhlist;
	}

	@Override
	public List<Medicine> searchByZhId(int id) {
		// TODO Auto-generated method stub
		ResultSet rs = null;
		Medicine medicine;
		List<Medicine> mlist = new ArrayList<Medicine>();
		try {
			String sql = "select m.* from medicine m,zhenghoumedicine zm where zm.medicine_id=m.mid and zm.zhenghou_id=?";
			con = this.getConn();
			ps = con.prepareStatement(sql);
			ps.setInt(1, id);
			rs = ps.executeQuery();
			while (rs.next()) {
				medicine = new Medicine();
				medicine.setmId(rs.getInt("mid"));
				medicine.setmName(rs.getString("mname"));
				medicine.setmContent(rs.getString("mcontent"));
				mlist.add(medicine);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return mlist;
	}

	@Override
	public ZhengZhuang searchZZByZhid(int id) {
		// TODO Auto-generated method stub
		ResultSet rs = null;
		ZhengZhuang zhengzhuang=null;
		try {
			String sql = "select * from zhengzhuang where zzid=?";
			con = this.getConn();
			ps = con.prepareStatement(sql);
			ps.setInt(1, id);
			rs = ps.executeQuery();
			while (rs.next()) {
				zhengzhuang=new ZhengZhuang();
				zhengzhuang.setZzId(rs.getInt("zzid"));
				zhengzhuang.setZzNumber(rs.getInt("zznumber"));
				zhengzhuang.setZzName(rs.getString("zzname"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return zhengzhuang;
	}

}
